const { Readable, Writable } = require("stream");
// 可读流
const source = ["aa", "bb", "cc"];

class Read extends Readable {
  constructor(source) {
    super();
    this.data = source;
  }
  _read() {
    let data = this.data.shift() || null;
    this.push(data);
  }
}

let readStream = new Read(source);

// let data = null;
// while ((data = readStream.read()) !== null) {
//   console.log(data.toString());
// }

readStream.on("data", (chunk) => {
  console.log(chunk.toString());
});

class Write extends Writable {
  constructor() {
    super();
  }

  /**
   * @param {*} chunk 写入内容
   * @param {*} en 编码
   * @param {*} done 完成方法
   */
  _write(chunk, en, done) {
    process.stdout.write(chunk.toString()), process.nextTick(done);
  }
}

let writeStream = new Write();

writeStream.write("ff", () => {
  console.log("write done");
});
